package cn.cmcc.online.smsapi.ui.manager;

import android.content.Context;
import android.util.LruCache;
import android.view.ViewGroup;

import java.util.LinkedList;

import cn.cmcc.online.smsapi.ui.presenter.FlightPresenterImpl;
import cn.cmcc.online.smsapi.ui.presenter.MoviePresenterImpl;
import cn.cmcc.online.smsapi.ui.presenter.TrainPresenterImpl;
import cn.cmcc.online.smsapi.ui.util.ContentUtil;
import cn.cmcc.online.smsapi.ui.util.DataExchangeProxy;
import cn.cmcc.online.smsapi.ui.view.CarrierOperatorVistaImpl;
import cn.cmcc.online.smsapi.ui.view.CommonVistaImpl;
import cn.cmcc.online.smsapi.ui.view.EmailVistaImpl;
import cn.cmcc.online.smsapi.ui.view.FlightVistaImpl;
import cn.cmcc.online.smsapi.ui.view.MovieVistaImpl;
import cn.cmcc.online.smsapi.ui.view.TrainVistaImpl;
import cn.cmcc.online.smsapi.ui.view.VerificationCodeVistaImpl;
import hugo.weaving.DebugLog;

/**
 * Created by admin on 2017/6/9.
 */

public class CardViewFactory {
    public static final int VIEW_ID_COMMON = 1;
    public static final int VIEW_ID_HOTEL = 2;
    public static final int VIEW_ID_VERIFICATION = 3;
    public static final int VIEW_ID_CARRIER_OPERATOR = 4;
    public static final int VIEW_ID_FLIGHT = 5;
    public static final int VIEW_ID_TRAIN = 6;
    public static final int VIEW_ID_MOVIE = 7;
    public static final int VIEW_ID_EMAIL = 8;

    private final static int VIEW_TYPE_CACHE_MAX_SIZE = 4;
    private LruCache<Integer, LinkedList<IBaseView>> mViewTypeLists = new LruCache<>(VIEW_ID_EMAIL);

    @android.support.annotation.IdRes
    int ID_BUBBLE = 999999999;

    @DebugLog
    public IBaseView getCacheBaseView(int viewId, ViewGroup apView) {
        IBaseView baseView = null;
        LinkedList<IBaseView> viewTypeList = getListFromCache(viewId);
        if (viewTypeList == null) {
            viewTypeList = new LinkedList<>();
        }
        if (viewTypeList.size() >= VIEW_TYPE_CACHE_MAX_SIZE) {
            int index;
            int pos = 0;
            do {
                baseView = viewTypeList.pollFirst();
                viewTypeList.offerLast(baseView);
                index = ContentUtil.indexOfChild(baseView.getView(), apView, ID_BUBBLE);
                pos++;
            } while (index != -1 && pos < viewTypeList.size());

            if (index != -1) {// view used
                baseView = null;
            }
        }

        if (baseView == null) {
            baseView = getBaseView(viewId);
            viewTypeList.offerLast(baseView);
            putListToCache(viewId, viewTypeList);
            baseView.getView().setId(ID_BUBBLE);
        }

        return baseView;
    }

    @DebugLog
    public IBaseView getBaseView(int viewId) {
        IBaseView baseView = null;

        switch (viewId) {
            case VIEW_ID_VERIFICATION:
                baseView = new VerificationCodeVistaImpl(mContext);
                break;

            case VIEW_ID_COMMON:
            case VIEW_ID_HOTEL:
                baseView = new CommonVistaImpl(mContext);
                break;

            case VIEW_ID_CARRIER_OPERATOR:
                baseView = new CarrierOperatorVistaImpl(mContext);
                break;

            case VIEW_ID_EMAIL:
                baseView = new EmailVistaImpl(mContext);
                break;

            case VIEW_ID_TRAIN:
                baseView = new TrainVistaImpl(mContext, new TrainPresenterImpl(mContext));
                break;

            case VIEW_ID_FLIGHT:
                baseView = new FlightVistaImpl(mContext, new FlightPresenterImpl(mContext));
                break;

            case VIEW_ID_MOVIE:
                baseView = new MovieVistaImpl(mContext, new MoviePresenterImpl(mContext));
                break;

            default:
                break;
        }

        return baseView;
    }

    private CardViewFactory(Context context, IDataExchange dataExchange) {
        mContext = context;
        DataExchangeProxy.getInstance(dataExchange);
    }

    private static CardViewFactory sCardViewFactory = null;
    private static Context mContext;

    public static CardViewFactory getInstance() {
        return sCardViewFactory;
    }

    public static CardViewFactory getInstance(Context context, IDataExchange dataExchange) {
        synchronized (CardViewFactory.class) {
            mContext = context;
            if (sCardViewFactory == null) {
                sCardViewFactory = new CardViewFactory(context, dataExchange);
            }
        }

        return sCardViewFactory;
    }

    private void putListToCache(int type, LinkedList<IBaseView> viewLinkedList) {
        if (type <= 0 || viewLinkedList == null || viewLinkedList.size() == 0) {
            return;
        }
        mViewTypeLists.put(type, viewLinkedList);
    }

    private LinkedList<IBaseView> getListFromCache(int type) {
        if (type <= 0) {
            return null;
        }
        return mViewTypeLists.get(type);
    }


    public void clearCacheData() {
        if (mViewTypeLists != null && mViewTypeLists.size() > 0) {
            mViewTypeLists.evictAll();
        }
    }

}
